// Authors: Matthew DeNovio
#include "Camera.h"

// Constructor
Camera::Camera()
{

}


// Initialize the Camera
void Camera::init()
{

	// Gets the Windows handle

	Camera::hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );

	// Changes Font Size // citing http://www.cplusplus.com/forum/windows/48863/ / http://msdn.microsoft.com/en-us/library/windows/desktop/ms686200%28v=vs.85%29.aspx
	COORD FontSize = {4, 6};
	CONSOLE_FONT_INFOEX current = {0}; // zero all members
	current.cbSize = sizeof(CONSOLE_FONT_INFOEX);
	current.dwFontSize = FontSize;
	SetCurrentConsoleFontEx(hConsole, false, &current);

	// Set Screen Buffer
	COORD pos = {locationScreenTileWidth * TILE_DIM, locationScreenTileHeight * TILE_DIM};
	SetConsoleScreenBufferSize(Camera::hConsole, pos);

	// Set Starting Height/Width of Console
	screenRect.Left = 0;
	screenRect.Top = 0;
	screenRect.Right = SCREEN_TILE_WIDTH * TILE_DIM - 1;
	screenRect.Bottom = SCREEN_TILE_HEIGHT * TILE_DIM - 1;	// -1 because otherwise its 1 bigger
	SetConsoleWindowInfo(Camera::hConsole,true,&screenRect);

	// Set Cursor To be invisible
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = 10;
	cursor.bVisible = false;
	SetConsoleCursorInfo(Camera::hConsole, &cursor);

	topLeft = Point(0,0);
	bottomRight = Point(SCREEN_TILE_WIDTH * TILE_DIM, SCREEN_TILE_HEIGHT * TILE_DIM);
}

// Deconstructor
Camera::~Camera(void)
{
	std::cout << "Deleted Camera" << std::endl;
}

// Change the 'Location' Size
void Camera::changeScreenBuffer()
{
	// Set the ScreenBuffer to be bigger "Scroll ability"

}

// Moves the Cursor to the given position
void Camera::setCursorPosition(U16 x, U16 y)
{
	COORD pos = {x, y};
	SetConsoleCursorPosition ( hConsole, pos );
}

// Move Camera
void Camera::moveCamera(U16 x, U16 y)
{
	topLeft.x = x * TILE_DIM;
	topLeft.y = y * TILE_DIM;
	bottomRight.x = topLeft.x + SCREEN_TILE_WIDTH * TILE_DIM;
	bottomRight.y = topLeft.y + SCREEN_TILE_HEIGHT * TILE_DIM;

	refocus();
}


// Moves the Camera by the amount of influence
bool Camera::shiftCamera(I16 x, I16 y)
{
	if(x)
	{
		topLeft.x += x;
		bottomRight.x += x;
	}

	else if(y)
	{
		topLeft.y += y;
		bottomRight.y += y;
	}

	refocus();

	return !(topLeft.x % TILE_DIM == 0 && topLeft.y % TILE_DIM == 0);
}

// Refocuses Camera
void Camera::refocus()
{
	setCursorPosition(topLeft.x, topLeft.y);
	setCursorPosition(bottomRight.x - 1, bottomRight.y - 1);	// Because Grid tiles start at not - 1
	std::cout << "";
}

void Camera::removeAnnoyingLines(const Point& screenShift)
{

	screenRect.Left = topLeft.x;
	screenRect.Right = bottomRight.x - 1;
	screenRect.Top = topLeft.y;
	screenRect.Bottom = bottomRight.y - 1;
	
	if(screenShift.x < 0)
	{
		screenRect.Left -= 1;
		screenRect.Right -= 1;
	}


	SetConsoleWindowInfo(Camera::hConsole,true,&screenRect);
	//
}

